# -*- coding: utf-8 -*-
# @Time : 2022/1/4 16:49
# @Author : Limusen
# @File : excel_utils

import xlrd3


class ExcelUtils:

    def __init__(self, excel, sheet_name=None):
        self.excel_path = excel
        self.sheet_name = sheet_name
        self.sheet_data = self.__get_all_data()

    def __get_all_data(self):
        """
        创建excel对象
        :return:
        """
        work_book = xlrd3.open_workbook(self.excel_path)
        if self.sheet_name:
            sheet = work_book.sheet_by_name(self.sheet_name)
        else:
            sheet = work_book.sheet_by_index(0)
        return sheet

    @property
    def get_count_rows(self):
        """
        获取excel总行数
        :return:  返回总行数
        """
        row_count = self.sheet_data.nrows
        return row_count

    @property
    def get_count_cols(self):
        """
        获取excel总列数
        :return:  返回总列数
        """
        cols_count = self.sheet_data.ncols
        return cols_count

    def get_sheet_data_by_list(self):
        """
        将数据用列表组成
        :return:
        """
        all_excel_data = []
        for row in range(self.get_count_rows):
            row_data = []
            for col in range(self.get_count_cols):
                cell_value = self.sheet_data.cell_value(row, col)
                row_data.append(cell_value)
            all_excel_data.append(row_data)
        return all_excel_data


if __name__ == '__main__':
    file_path = r"D:\PythonSelenium\sample\files\baidu_page.xls"
    excel = ExcelUtils(file_path)
    print(excel.get_sheet_data_by_list())
